package part_09_A_Worked_Example;

import java.util.LinkedList;
import java.util.List;
import java.util.Random;

public class Processor {

	LinkedList<Integer> lint = new LinkedList<Integer>();
	final int LIMIT = 10;
	private Object lock = new Object();

	void produce() throws InterruptedException {

		int number = 0;

		while (true) {

	synchronized (lock) {
		
while(lint.size()==LIMIT){lock.wait();}		
		lint.add(number++);
		lock.notify();
		
	}
	
		}

	}

	void consume() throws InterruptedException {

		Random  rnumber=new Random();
		while (true) {

			synchronized (lock) {
				System.out.println("lint.size() :: "+lint.size()); 
				while(lint.size()==0){
				lock.wait();	
					
				}
				int value = lint.removeFirst();
				System.out.println(value);
				lock.notify();
							
			}
			Thread.sleep(rnumber.nextInt(1000));	
		}//while ends

		
		
	}

}
